package cn.itcast.bos.service.base.impl;

import java.util.Date;
import java.util.Iterator;
import java.util.UUID;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.AreaRepository;
import cn.itcast.bos.dao.base.IFixedAreaDao;
import cn.itcast.bos.dao.base.IOrderDao;
import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.SubArea;
import cn.itcast.bos.domain.delivery.Order;
import cn.itcast.bos.service.base.IOrderService;

@Transactional
@Service
public class OrderServiceImpl implements IOrderService {
	@Autowired
	private IFixedAreaDao fixedAreaDao;
	
	@Autowired
	private IOrderDao orderDao;
	
	@Autowired
	private AreaRepository areaRepository;
	
	//抽离的自动分单方法
	private void saveOrder(Order order, Courier courier) {
		order.setCourier(courier);
		order.setOrderType("1");
		orderDao.save(order);
		System.out.println("自动分单成功");
	}
	
	@Override
	public void saveOrder(Order order) {
		order.setOrderNum(UUID.randomUUID().toString());
		Date orderTime = new Date();
		order.setOrderTime(orderTime);
		order.setStatus("1");

		//寄件人省市区
		Area sendArea = order.getSendArea();
		Area persistSendArea = areaRepository.findByProvinceAndCityAndDistrict(sendArea.getProvince(), sendArea.getCity(), sendArea.getDistrict());

		//收件人省市区
		Area recArea = order.getRecArea();
		Area persistRecArea = areaRepository.findByProvinceAndCityAndDistrict(recArea.getProvince(), recArea.getCity(), recArea.getDistrict());
		System.out.println("sendArea:" + sendArea + "persistSendArea:" + persistSendArea);
		System.out.println("recArea:" + recArea + "persistRecArea:" + persistRecArea);
		
		order.setSendArea(persistSendArea);
		order.setRecArea(persistRecArea);
	
		
		String fixedAreaId = WebClient.create("http://localhost:9002/crm_management/services/"
				+ "customerService/customer/fixedAreaIdByAddress?address=" + order.getSendAddress()).accept(MediaType.APPLICATION_JSON).get(String.class);
		if (fixedAreaId != null) {
			FixedArea fixedArea = fixedAreaDao.findOne(fixedAreaId);
			System.out.println(fixedArea);
			System.out.println(fixedArea.getCompany() + ":" + fixedArea.getOperatingCompany());
			//Courier courier = fixedArea.getCouriers().iterator().next();
			Iterator<Courier> iterator = fixedArea.getCouriers().iterator();
			if (iterator.hasNext()) {
				Courier courier = fixedArea.getCouriers().iterator().next();
				System.out.println(courier);
				if (courier != null) {
					saveOrder(order, courier);
					return;
				}
			} else {
				System.out.println("没有合适的快递员");
			}
		}
		
		//基于分区实现自动分单：通过省市区查询分区关键字匹配地址
		for (SubArea subArea : persistSendArea.getSubareas()) {
			System.out.println("subArea:" + subArea);
			if (order.getSendAddress().contains(subArea.getKeyWords())) {
				Courier courier = subArea.getFixedArea().getCouriers().iterator().next();
				if (courier != null) {
					saveOrder(order, courier);
					return;
				}
			}
		}
		
		//查询副主关键字
		for (SubArea subArea : persistSendArea.getSubareas()) {
			System.out.println("subArea:" + subArea);
			if (order.getSendAddress().contains(subArea.getAssistKeyWords())) {
				Courier courier = subArea.getFixedArea().getCouriers().iterator().next();
				if (courier != null) {
					saveOrder(order, courier);
					return;
				}
			}
		}
		
		order.setOrderType("2");
		orderDao.save(order);
	}
	
	public void moNiDuanXin(){
		System.out.println("短信发送成功");
	}

	@Override
	public Order findByOrderNum(String orderNum) {
		return orderDao.findByOrderNum(orderNum);
	}

}
